﻿using System;
using System.Runtime.Serialization;
using System.ComponentModel;
using System.Text;

namespace BrainTechLLC.ThreadSafeObjects
{
    [Flags]
    [DataContract]
    public enum LogSeverity
    {
        [EnumMember]
        DebugInformation = 0,
        [EnumMember]
        DebugWarning = 1,
        [EnumMember]
        DebugError = 2,
        [EnumMember]
        DebugCritical = 4,
        [EnumMember]
        Information = 8,
        [EnumMember]
        Warning = 16,
        [EnumMember]
        Error = 32,
        [EnumMember]
        Critical = 64,
        [EnumMember]
        All = (int)0x1fffffff
    }

#if NO_SILVERLIGHT
    [Browsable(true)]
    [TypeConverter(typeof(ExpandableObjectConverter))]
#endif
    public interface ILogger
    {
        event EventHandler<ObjectEventArgs<LogMessage>> LoggedMessage;
        bool AddLogListener(ILogListener listener);
        bool RemoveLogListener(ILogListener listener);
        void Log(string messageType, string message, LogSeverity severity);
        void Log(Exception ex, LogSeverity severity, bool verbose);
        void Log(Exception ex, string message, LogSeverity severity, bool verbose);
        void Log(LogMessage message);
        void SetFilter(LogSeverity severity, bool logThisSeverityLevel);

        bool WantExit { get; set; }
        void BeginLogging();
        LogSeverity LogFilter { get; }
    }

    [DataContract]
    [Serializable]
    public class LogMessage
    {
        [DataMember]
        public DateTime LogTime { get; set; }

        [DataMember]
        public string Message { get; set; }

        [DataMember]
        public LogSeverity Severity { get; set; }

        [DataMember]
        public string MessageType { get; set; }

        public override string ToString()
        {
            return FormatLogEntry(LogTime.ToString(), MessageType, Message, Severity);
        }

        public string FormatLogEntry(string nowString, string messageType, string message, LogSeverity severity)
        {
            StringBuilder sb = new StringBuilder(256);
            sb.Append(nowString);
            sb.Append(',');
            sb.Append(messageType);
            sb.Append(',');
            sb.Append(severity.ToString());
            sb.Append(',');
            sb.Append(message);
            return sb.ToString();
        }
    }
}
